//
//  PublishCell.m
//  soccer
//
//  Created by 上官千城 on 15/9/7.
//  Copyright (c) 2015年 上官千城. All rights reserved.
//

#import "OPublishCell.h"

@implementation OPublishCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self && [reuseIdentifier isEqualToString:@"settingCell"]) {
        [self creatCell];
    }
    if (self && [reuseIdentifier isEqualToString:@"textCell"]) {
        [self creatTextField];
    }
   
    return self;
}
//2区
-(void)creatTextField
{

    
    
    self.texView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, WIDTH - 20, 180)];
    self.texView.layer.cornerRadius = 15;
    self.texView.keyboardType = UIKeyboardTypeDefault;
    self.texView.layer.masksToBounds = YES;
    self.texView.delegate = self;
    self.texView.layer.borderWidth = 1;
    self.texView.layer.borderColor = [UIColor grayColor].CGColor;
    self.texView.font = [UIFont systemFontOfSize:18];
    self.texView.backgroundColor = [UIColor whiteColor];
    self.texView.scrollEnabled = NO;
    self.texView.textColor = [UIColor blackColor];
    self.texView.autoresizesSubviews = UIViewAutoresizingFlexibleHeight;
    self.texView.text = [DataBaseHandel shareBaseData].dataText;

    [self.contentView addSubview:self.texView];
    UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, self.texView.frame.size.height + self.texView.frame.origin.y + 10, WIDTH, 50)];
    aView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:aView];
    
    
    
    self.publishButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.publishButton.frame = CGRectMake(10, aView.frame.size.height + aView.frame.origin.y + 10, WIDTH-20, 30);
    [self.publishButton setTitle:@"确认发布" forState:UIControlStateNormal];
    [self.publishButton addTarget:self action:@selector(pulishiAction) forControlEvents:UIControlEventTouchUpInside];
    self.publishButton.layer.masksToBounds = YES;
    self.publishButton.layer.cornerRadius = 5;
    self.publishButton.backgroundColor = [UIColor greenColor];
    [self.contentView addSubview:self.publishButton];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardReturn) name:@"RETURN" object:nil];
    
    
    
}

-(void)textViewDidEndEditing:(UITextView *)textView
{
    [DataBaseHandel shareBaseData].dataText = textView.text;
}


-(void)dealloc
{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"RETURN" object:nil];
    
    
}

-(void)keyBoardReturn
{


    [self.contentView endEditing:YES];
}





//1区
-(void)creatCell
{
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.pictureArray = [NSMutableArray array];
    self.addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.addButton.frame = CGRectMake(10, 10, 70, 70);
    [self.addButton setImage:[UIImage imageNamed:@"添加图片@2x"] forState:UIControlStateNormal];
    [self.addButton addTarget:self action:@selector(addPicAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.addButton];
    
    self.soundButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.soundButton.frame = CGRectMake(self.addButton.frame.origin.x + self.addButton.frame.size.width + 10, self.addButton.frame.origin.y, self.addButton.frame.size.width, self.addButton.frame.size.height);
    [self.soundButton setImage:[UIImage imageNamed:@"添加录音"] forState:UIControlStateNormal];
    [self.soundButton addTarget:self action:@selector(soundButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.soundButton];
    
}

-(void)setPictureArray:(NSMutableArray *)pictureArray
{

    _pictureArray = pictureArray;
    
    for (int i = 0; i < pictureArray.count; i++) {

        OPublishModel *publishM = [pictureArray objectAtIndex:i];
        UIImageView *pickerImageV = [[UIImageView alloc] initWithFrame:CGRectMake(i%4*80+10 + 10, i/4*80 + 10, self.addButton.frame.size.width, self.addButton.frame.size.height)];
        pickerImageV.backgroundColor = [UIColor orangeColor];
        pickerImageV.tag = 4000 + i;
        pickerImageV.image = publishM.image;
        pickerImageV.userInteractionEnabled = NO;
        NSLog(@"IMAGEDATA === %@",pickerImageV.image);
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(30, 30, 15, 15);
        btn.layer.cornerRadius = 7.5;
        btn.center = CGPointMake(pickerImageV.frame.origin.x +pickerImageV.frame.size.width, pickerImageV.frame.origin.y);
        btn.backgroundColor = [UIColor redColor];
        btn.tag = 5000 + i;
        [btn addTarget:self action:@selector(imageBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:pickerImageV];
        [self.contentView addSubview:btn];
    }

    self.addButton.frame = CGRectMake(pictureArray.count%4*80+10 + 10, pictureArray.count/4*80 + 10, self.addButton.frame.size.width, self.addButton.frame.size.height);
    self.soundButton.frame = CGRectMake((pictureArray.count + 1)%4*80+10 + 10, (pictureArray.count + 1)/4*80 + 10, self.addButton.frame.size.width, self.addButton.frame.size.height);

}



//适应cell高度
-(CGRect)frameForCell
{

    return self.soundButton.frame;
}


-(void)imageBtnAction:(UIButton *)btn
{
    
     [self.pictureArray removeObjectAtIndex:btn.tag - 5000];
    
    [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    for (int i = 0; i < self.pictureArray.count; i++) {
        
        OPublishModel *publishM = [self.pictureArray objectAtIndex:i];

        UIImageView *pickerImageV = [[UIImageView alloc] initWithFrame:CGRectMake(i%4*80+10 + 10, i/4*80 + 10, self.addButton.frame.size.width, self.addButton.frame.size.height)];
        pickerImageV.backgroundColor = [UIColor orangeColor];
        pickerImageV.tag = 4000 + i;
        pickerImageV.image = publishM.image;
        pickerImageV.userInteractionEnabled = NO;
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(30, 30, 15, 15);
        btn.layer.cornerRadius = 7.5;
        btn.center = CGPointMake(pickerImageV.frame.origin.x +pickerImageV.frame.size.width, pickerImageV.frame.origin.y);
        btn.backgroundColor = [UIColor redColor];
        btn.tag = 5000 + i;
        [btn addTarget:self action:@selector(imageBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:pickerImageV];
        [self.contentView addSubview:btn];
    }
   self.addButton.frame = CGRectMake(self.pictureArray.count%4*80+10 + 10, self.pictureArray.count/4*80 + 10, self.addButton.frame.size.width, self.addButton.frame.size.height);

    [self.contentView addSubview:self.addButton];
    
    
    self.soundButton.frame = CGRectMake((self.pictureArray.count + 1)%4*80+10 + 10, (self.pictureArray.count + 1)/4*80 + 10, self.addButton.frame.size.width, self.addButton.frame.size.height);

    [self.contentView addSubview:self.soundButton];
    [self.myDelegate deleteImage:self.pictureArray];
 
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{

    if ([text isEqualToString:@"\n"]) {
        [self.texView resignFirstResponder];
        return NO;
    }
    return YES;
}


//添加语音
-(void)soundButtonAction
{
    NSLog(@"语音");
}
//添加图片
-(void)addPicAction
{
    
    NSLog(@"添加");
 

    [self.myDelegate pushPhoto];
}

//3区发布方法
-(void)pulishiAction
{
//    if ([self.texView.text isEqualToString:@""]) {
//        [UIUtils showAlert:@"提示" message:@"内容不能为空" left:@"确定" right:nil delegate:nil];
//        return;
//    }
    
    [self.myDelegate publishText:self.texView.text];

//    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"确认要发布么?" delegate:self cancelButtonTitle:@"是" otherButtonTitles:@"否", nil];
//    [alert show];
    
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        NSLog(@",,,,,,,,,,%@",self.texView.text);
        [self.myDelegate publishText:self.texView.text];


    }
    else if (buttonIndex == 1)
    {
        NSLog(@"否");
    }
}


@end
